package com.lookoutthebush.companyindex.dao;

import com.lookoutthebush.companyindex.domain.NewsDO;
import com.lookoutthebush.companyindex.util.MyJdbcDAO;
import com.lookoutthebush.companyindex.util.PageHelper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * NewsDAO 资讯dao
 *
 * @author LookOutTheBush
 * @date 2021/9/14 9:05 下午
 */
@Repository
public class NewsDAO {

    @Resource
    private MyJdbcDAO jdbcDAO;

    public Long insert(NewsDO newsDO) {
        jdbcDAO.create(newsDO);
        return newsDO.getId();
    }

    public NewsDO queryById(Long id) {
        return jdbcDAO.queryById(id, NewsDO.class);
    }

    public boolean update(NewsDO newsDO) {
        jdbcDAO.update(newsDO);
        return true;
    }

    /**
     * 查询未删除的
     *
     * @param typeIds 类型ids
     * @param release 是否发布
     * @return list
     */
    public PageHelper<NewsDO> queryValidListByPage(List<Long> typeIds, Integer release, PageHelper<NewsDO> page) {
        String sql =
                "select * from news where typeid in (:typeIds) and delete = 0 and release = :release order by id desc";
        Map<String, Object> param = new HashMap<>(4);
        param.put("typeId", typeIds);
        param.put("release", release);
        return jdbcDAO.queryListByPage(sql, param, NewsDO.class, page);
    }
}
